const Category = require("../model/CategoryModel");

const attributes = ["Id", "CategoryName", "IsHot", "Sort"];

class CategoryDao {
  async save(data) {
    const res = await Category.create(data);
    return res.toJSON();
  }

  async update(data) {
    let { Id, CategoryName, IsHot, Sort } = data;
    const res = await Category.update(
      { CategoryName, IsHot, Sort },
      {
        where: {
          Id,
        },
      }
    );
    return true;
  }

  async getOneById(Id) {
    const res = await Category.findOne({
      attributes,
      where: {
        Id,
      },
    });
    return res.toJSON();
  }

  async delete(Id) {
    const res = await Category.destroy({
      where: {
        Id,
      },
    });
    return true;
  }

  async list() {
    const res = await Category.findAll({
      attributes,
      order: [["Sort", "ASC"]],
    });
    console.log(res);
    return res;
  }
}
module.exports = new CategoryDao();
